package com.a.l;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JwtDomain {


    public static JWTResp getJWT(JWTReq jwtReq, Context context) {
        if (jwtReq == null) {
            JWTResp resp = new JWTResp();
            resp.setResultCode("500");
            resp.setResultMsg("req  is null");
            return resp;
        }
        if (jwtReq.getKkPid() == null || "".equals(jwtReq.getKkPid())) {
            JWTResp resp = new JWTResp();
            resp.setResultCode("500");
            resp.setResultMsg("kkPid is null");
            return resp;
        }
        if (jwtReq.getKkAppId() == null || "".equals(jwtReq.getKkAppId())) {
            JWTResp resp = new JWTResp();
            resp.setResultCode("500");
            resp.setResultMsg("appId is null");
            return resp;
        }
        if (jwtReq.getDevMac() == null || "".equals(jwtReq.getDevMac())) {
            JWTResp resp = new JWTResp();
            resp.setResultCode("500");
            resp.setResultMsg("devMac is null");
            return resp;
        }
        SharedPreferences sp = context.getSharedPreferences(
                "FlutterSharedPreferences",
                Context.MODE_MULTI_PROCESS);
        String ccu = sp.getString("flutter.jwt_current_ccu", "");
        Log.e("JwtDomain", "ccu info  : " + ccu);
        if (ccu == null || "".equals(ccu)) {
            JWTResp resp = new JWTResp();
            resp.setResultCode("500");
            resp.setResultMsg("server not login");
            return resp;
        }
        String authCode = null;
        String deviceId = null;
        String accessToken = null;
        try {
            JSONObject object = new JSONObject(ccu);
            authCode = object.getString("authCode");
            String qrcode = object.getString("qrcode");
            deviceId = object.getString("deviceId");
            accessToken = object.getString("accessToken");
        } catch (JSONException e) {
            e.printStackTrace();
            if (ccu == null || "".equals(ccu)) {
                JWTResp resp = new JWTResp();
                resp.setResultCode("500");
                resp.setResultMsg(""+e.getMessage());
                return resp;
            }
        }
        String jwtToken = JwtUtil.getJWTToken(authCode, deviceId, jwtReq.getKkAppId(), Integer.parseInt(jwtReq.getKkPid()),  jwtReq.getDevMac());

        doHttp(jwtToken, accessToken);

        try {
            JSONObject object = new JSONObject();
            object.put("devMac",jwtReq.getDevMac());
            object.put("kkAppId", jwtReq.getKkAppId());
            object.put("kkPid", Integer.parseInt(jwtReq.getKkPid()));
            sp.edit().putString("flutter.jwt_req_args", object.toString()).commit();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JWTResp jwtResp = new JWTResp(jwtToken, "200", "success", deviceId);
        return jwtResp;
    }

    private static void doHttp(String jwtToken, String accessToken) {
       new Thread(() -> {
           String url = "https://aispeech.ikonke.com/auth/auth?jwt=" + jwtToken;
           HttpUtil.doGet(url, accessToken);
       }).start();

    }

}
